<?php
/*
 * template/load-index.phtml
 */
?>
<?php

use RHX\Helper\RHDateTime;

// Variables
$templates	 = $this->templates;
$site		 = $this->site;
?>
<div class="list-container">
    <table class="table table-striped list-table">
	<thead>
	    <tr>
		<td width="5%">Num</td>
		<td width="30%">Libellé</td>
		<td width="25%">Site</td>
		<td width="15%">Date Création</td>
		<td width="15%">Date Modification</td>
		<td width="10%">&nbsp;</td>
	    </tr>
	</thead>
	<tbody>
	    <?php if ($templates->count() > 0) : ?>
		<?php foreach ($templates as $t): ?>
		    <?php
		    $creation	 = new RHDateTime($t->creation);
		    $modification	 = new RHDateTime($t->edition);
		    ?>
			    <tr>
			<?php $version	 = 1/*$t->getVersion()*/; ?>
			<td><a href='#' class='editTemplate' data-id='<?php echo $t->id; ?>' data-version="<?php echo $version; ?>" id="template-<?php echo $t->template; ?>"><?php echo $t->template; ?></a></td>
			<td><a href='#' class='editTemplate' data-id='<?php echo $t->id; ?>' data-version="<?php echo $version; ?>"><?php echo $t->libelle; ?></a></td>
			<td><a href='#' class='editTemplate' data-id='<?php echo $t->id; ?>' data-version="<?php echo $version; ?>"><?php echo $site->libelle; ?></a></td>
			<td><a href='#' class='editTemplate' data-id='<?php echo $t->id; ?>' data-version="<?php echo $version; ?>"><?php echo $creation->dateFr(); ?></a></td>
			<td><a href='#' class='editTemplate' data-id='<?php echo $t->id; ?>' data-version="<?php echo $version; ?>"><?php echo $modification->dateFr(); ?></a></td>
			<td>
			    <a href='#' class='deleteTemplate' data-id='<?php echo $t->id; ?>'>
				<img src='<?php echo $this->basePath() . '/img/rhx/delete-16x16.png' ?>' alt='Effacer' title='Effacer'>
			    </a>
			</td>
		    </tr>
		<?php endforeach; ?>
	    <?php else: ?>
    	    <tr>
    		<td colspan="5">Il n'y a pas de Planning Type pour ce Site</td>
    		<td>
    		    <button class='btn btn-success addTemplate2'>Ajouter</button>
    		</td>
    	    </tr>
	    <?php endif; ?>
	</tbody>
    </table>
</div>

<script type='text/javascript'>
    // Démarrage
    $(function() {
	// Add Template
	$('.addTemplate2').click(function(e) {
	    e.preventDefault();
	    $('.addTemplate').click();
	});
	// Edit Template
	$('.editTemplate').click(function(e) {
	    e.preventDefault();
	    id = $(this).attr('data-id');
	    version = $(this).attr('data-version');
	    ajax = $.ajax({
		type: 'post',
		url: '<?php echo $this->url('RHX/default', array('controller'	 => 'template', 'action'	 => 'edit')) ?>/' + id,
		data: '&nohead=0&fullscreen=1&version=' + version
	    });
	    ajax.done(function(data) {
		fullScreenLayout();
		$('#fullScreen').hide().delay(500).html(data).fadeIn('normal');
	    });
	    ajax.fail(function() {
		alert('Erreur de chargement');
	    });
	});
	// Delete template
	$('.deleteTemplate').click(function(e) {
	    e.preventDefault();
	    if (!confirm("Voulez-vous effacer le Planning Type ?")) {
		return false;
	    }
	    id = $(this).attr('data-id');
	    ajax = $.ajax({
		type: 'post',
		url: '<?php echo $this->url('RHX/default', array('controller'	 => 'template', 'action'	 => 'erase')) ?>',
		data: '&id=' + id
	    });
	    ajax.done(function(result) {
		if (!result.done) {
		    alert("Une erreur est survenue lors de la suppression du planning");
		    alert(result.error);
		}
		refresh();
	    });
	});
    });
</script>